package sort;

import java.util.ArrayList;
import java.util.Arrays;

public class Radix {
    public static void main(String[] args) {
        logarithm(100000, 1000, 1000);
        runTime(1, 10000000, 1000000);
    }

    /**
     * 桶排序算法， 平均 O(n*k), 最好 O(n*k), 最坏 O(n*k) 空间复杂度 O(n+k) out-place 稳定
     *
     * @param array
     * @return
     */
    public static void radix(int[] array) {
        int[][] bucket = new int[10][array.length];
        int[] bucketElementCounts = new int[10];
        int numMax = array[0];
        for (int i = 0; i < array.length; i++) {
            if (array[i] > numMax) {
                numMax = array[i];
            }
        }
        int maxLength = (numMax + "").length();
        for (int i = 0, n = 1; i < maxLength; i++, n *= 10) {
            for (int j = 0; j < array.length; j++) {
                int digitOfElement = array[j] / n % 10;
                bucket[digitOfElement][bucketElementCounts[digitOfElement]] = array[j];
                bucketElementCounts[digitOfElement]++;
            }
            int index = 0;
            for (int k = 0; k < bucketElementCounts.length; k++) {
                if (bucketElementCounts[k] != 0) {
                    for (int l = 0; l < bucketElementCounts[k]; l++) {
                        array[index++] = bucket[k][l];
                    }
                }
                bucketElementCounts[k] = 0;
            }
        }
    }

    /**
     * 打印数组
     *
     * @param array
     */
    public static void show(int[] array) {
        for (int i = 0; i < array.length; i++) {
            System.out.print(array[i] + " ");
        }
        System.out.println();
    }

    /**
     * 产生随机数组
     *
     * @param length
     * @return
     */
    public static int[] getRandomArray(int length, int numRange) {
        int[] array = new int[length];
        for (int i = 0; i < length; i++) {
            array[i] = (int) (Math.random() * numRange);
        }
        return array;
    }

    /**
     * 对数器
     *
     * @param testNum
     * @param arrayLen
     * @param numRange
     */
    public static void logarithm(int testNum, int arrayLen, int numRange) {
        boolean runResult = true;
        for (int i = 0; i < testNum; i++) {
            int[] randomArray = getRandomArray(arrayLen, numRange);
            boolean result = logarithmRun(randomArray);
            if (!result) {
                runResult = false;
            }
        }
        if (runResult) {
            System.out.println(true);
        }
    }

    /**
     * 对数器计算过程
     *
     * @param array
     * @return
     */
    public static boolean logarithmRun(int[] array) {
        //复制传入样本
        int[] newArray = new int[array.length];
        for (int i = 0; i < newArray.length; i++) {
            newArray[i] = array[i];
        }
        //获取实际算法输出
        radix(array);
        int[] actualVal = array;
        //获取期望输出
        Arrays.sort(newArray);
        int[] expectedVal = newArray;
        //核对结果
        boolean equals = isEquals(actualVal, expectedVal);
        if (equals) {
            return true;
        } else {
            System.out.println("expectedVal:");
            show(expectedVal);
            System.out.println("actualVal:");
            show(actualVal);
            return false;
        }
    }

    /**
     * 结果核对
     *
     * @param actualVal
     * @param expectedVal
     * @return
     */
    public static boolean isEquals(int[] actualVal, int[] expectedVal) {
        if (actualVal.length != expectedVal.length) {
            return false;
        }
        if (actualVal != null && expectedVal == null || actualVal == null && expectedVal != null) {
            return false;
        }
        for (int i = 0; i < actualVal.length; i++) {
            if (actualVal[i] != expectedVal[i]) {
                return false;
            }
        }
        return true;
    }

    /**
     * 算法运行时间
     *
     * @param testNum
     * @param arrayLen
     * @param numRange
     */
    public static void runTime(int testNum, int arrayLen, int numRange) {
        long startTime = System.currentTimeMillis();
        for (int i = 0; i < testNum; i++) {
            int[] randomArray = getRandomArray(arrayLen, numRange);
            radix(randomArray);
        }
        long endTime = System.currentTimeMillis();
        long consumingTime = endTime - startTime;
        System.out.println("startTime:" + startTime + "\nendTime:" + endTime + "\nconsumingTime:" + consumingTime);
    }
}
